//
//  YWDownloadController3.m
//  videoTest3
//
//  Created by jun peng on 2022/8/15.
//

#import "YWDownloadController3.h"

#import "YWSectionGroupStatus.h"
#import "YWDownloadStatus3.h"
#import "YWDownloadCell3.h"

#import "YWDownloadManager3.h"
#import "YWDownloadDataTaskTool.h"
#import "YWDownloadDataBaseTool.h"

#import "YWNoContentView2.h"
#import "YWFinishDownloadToPlayController.h"

#import "YWKLineCheatsVideoListDataStatus.h"

@interface YWDownloadController3 ()<UITableViewDelegate,UITableViewDataSource,YWDownloadCell3Delegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *barConstraintHeight;
@property (weak, nonatomic) IBOutlet YWTableView *tableView;
@property (nonatomic,strong)NSArray *group;
@property(nonatomic,weak)UIButton *rightItem;

@end

@implementation YWDownloadController3


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBar.title = @"下载列表";
    self.barConstraintHeight.constant = self.navBar.frame.size.height;
    [self setupRightItem];
    
    //获取数据
    YWDownloadManager3 *manager = [YWDownloadManager3 sharedownloadManager];
    MJWeakSelf
    [manager getDataTaskArray:^(NSArray * _Nonnull group) {
        
        //假如group的数据有变化、都会走block、从而刷新列表
        weakSelf.group = group;
        [weakSelf.tableView reloadData];
        
        YWSectionGroupStatus *sectionStatus0 = weakSelf.group[0];
        YWSectionGroupStatus *sectionStatus1 = weakSelf.group[1];
        
        if (sectionStatus0.statusArray.count == 0 && sectionStatus1.statusArray.count == 0) {
            //这里弄个noCotentView
            [weakSelf.view addSubview:weakSelf.noContentView];
            weakSelf.noContentView.frame = CGRectMake(0, weakSelf.navBar.height, Wi, weakSelf.view.height - weakSelf.navBar.height);
            weakSelf.noContentView.noContentType = YWNoContentView2Type_NoContentButNotConformBtn;
        }
        else{
            //移除noCotentView
            [weakSelf.noContentView removeFromSuperview];
        }
    }];
    
}


//按cell的暂停继续按钮代理
-(void)downloadCell3:(YWDownloadCell3 *)downloadCell didClickSituationBtnWithStatus:(YWDownloadStatus3 *)itemStatus{
    
    YWDownloadManager3 *manager = [YWDownloadManager3 sharedownloadManager];
    
    //具体做法应该交给Manager处理
    if (itemStatus.downloadType == YWDownloadType_Downloading) {
        [manager pauseOneDataTaskWithStatus:itemStatus];
    }
    else if (itemStatus.downloadType == YWDownloadType_Pause) {
        [manager continueOneDataTaskWithStatus:itemStatus];
    }
    else if (itemStatus.downloadType == YWDownloadType_Waiting) {
        [manager advanceOneDataTaskWithStatus:itemStatus];
    }
    else if (itemStatus.downloadType == YWDownloadType_Error3) {
        [manager continueOneDataTaskWithStatus:itemStatus];
    }
}

//按cell的删除按钮
-(void)cellDidClickDeleteWithIndexPath:(NSIndexPath *)indexPath{
    
    YWDownloadManager3 *manager = [YWDownloadManager3 sharedownloadManager];
    
    YWSectionGroupStatus *sectionStatus = self.group[indexPath.section];
    
    YWDownloadStatus3 *status = sectionStatus.statusArray[indexPath.row];
    
    if (indexPath.section == 0) { //正在任务中的视频
        
        [manager removeOneDownloadingDataTaskWithStatus:status];
    }
    else{  //已经完成了的视频
        
        [manager removeOneFinishDataTaskWithStatus:status];
        
    }
}

//---------------------------------------------------------------------------------------------------------------------------

-(void)setupRightItem{
    UIButton *rightItem = [[UIButton alloc] init];
    [rightItem setTitle:@"编辑" forState:UIControlStateNormal];
    [rightItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightItem.bounds = CGRectMake(0, 0, 100, 30);
    rightItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightItem setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 15)];
    [rightItem setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    self.navBar.rightItem = rightItem;
    rightItem.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightItem addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    self.rightItem = rightItem;
}
-(void)edit{
    self.tableView.editing = !self.tableView.editing;
    if (self.tableView.editing) {
        [self.rightItem setTitle:@"完成" forState:UIControlStateNormal];
    }
    else{
        [self.rightItem setTitle:@"编辑" forState:UIControlStateNormal];
    }
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 11.0, *)) {
        [self customDeleteBtnAfteriOS11:tableView];
    }
}
- (void)customDeleteBtnAfteriOS11:(UITableView *)tableView {
    for (UIView *subview in tableView.subviews) {
        if ([NSStringFromClass([subview class]) isEqualToString:@"UISwipeActionPullView"]) {
            //设置按钮frame
            for (UIView * sonView in subview.subviews) {
                if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                    sonView.frame = CGRectMake(-15, 0, 70, 70);
                    sonView.backgroundColor = YWColor(255, 110, 110);
                }
            }
        }
    }
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath {
    MJWeakSelf
    if (@available(iOS 11.0, *)) {
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *_Nonnullaction, NSIndexPath *_NonnullindexPath) {
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除？" preferredStyle:UIAlertControllerStyleAlert];

        [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //调用删除方法
            [weakSelf cellDidClickDeleteWithIndexPath:indexPath];
        }]];

        [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];

        [self presentViewController:alertVc animated:YES completion:nil];
    }];
    rowAction.backgroundColor = [UIColor clearColor];
    return @[rowAction];
    }
    else {
        UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *_Nonnullaction, NSIndexPath *_NonnullindexPath) {
            UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除？" preferredStyle:UIAlertControllerStyleAlert];

            [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //调用删除方法
                [weakSelf cellDidClickDeleteWithIndexPath:indexPath];
            }]];

            [alertVc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];

            [self presentViewController:alertVc animated:YES completion:nil];

        }];

        rowAction.backgroundColor = [UIColor clearColor];
        return @[rowAction];
    }
}

//---------------------------------------------------------------------------------------------------------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YWSectionGroupStatus *sectionStatus = self.group[indexPath.section];
    
    YWDownloadStatus3 *status = sectionStatus.statusArray[indexPath.row];
    
    YWDownloadCell3 *cell = [YWDownloadCell3 cellWithTableView:tableView];
    cell.delegate = self;
    cell.status = status;
    
    return cell;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.group.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    YWSectionGroupStatus *group = self.group[section];
    return group.statusArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 105;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    YWSectionGroupStatus *scetionStatus = self.group[section];
    
    //没数据就不要头部了
    if (scetionStatus.statusArray.count == 0 ) {
        
        return 1;
    }
    else{
        
        return 30;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 
    
    YWSectionGroupStatus *scetionStatus = self.group[section];
    
    //没数据就不要头部了
    if (scetionStatus.statusArray.count == 0 ) {
        
        return nil;
    }
    else{
        UILabel *label = [[UILabel alloc] init];
        label.text = scetionStatus.headerTitle;
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor blackColor];
        label.frame = CGRectMake(0, 0, Wi, 30);
        return label;
      
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    YWSectionGroupStatus *sectionStatus = self.group[indexPath.section];
    
    YWDownloadStatus3 *status = sectionStatus.statusArray[indexPath.row];
    
    if (status.downloadType != YWDownloadType_Finish3) {
        return;
    }
    
    
    YWFinishDownloadToPlayController *vc = [[YWFinishDownloadToPlayController alloc] initWithNibName:@"YWFinishDownloadToPlayController" bundle:nil];
    
    
    YWKLineCheatsVideoListDataStatus *cheatsStatus = [[YWKLineCheatsVideoListDataStatus alloc] init];
    
    cheatsStatus.Title = status.Title;
    cheatsStatus.PicUrl = status.PicUrl;
    cheatsStatus.Id = status.Id;
    cheatsStatus.VideoUrl = status.VideoUrl;
    
    vc.status = cheatsStatus;
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
